{extend name="index/base" /}
{block name="content"}
<blockquote class="layui-elem-quote fhui-admin-main_hd">
    <h2>
        文章列表
    </h2>
</blockquote>
<div class="y-role">
    <!--工具栏-->
    <div class="toolbar-wrap" id="floatHead">
        <div class="toolbar">
            <div class="box-wrap">
                <a class="menu-btn">
                </a>
                <div class="l-list">
                    <a class="layui-btn layui-btn-sm do-action" data-type="doAdd" href="{:url('addArticle')}">
                        <i class="fa fa-plus">
                        </i>
                        新增
                    </a>
                    <a class="layui-btn layui-btn-sm do-action" onclick="window.location.reload()">
                        <i class="fa fa-refresh fa-spin">
                        </i>
                        刷新
                    </a>
                </div>
            </div>
        </div>
    </div>
    <!--/工具栏-->
    <!--文字列表-->
    <div class="fhui-admin-table-container">
        <table id="table1" lay-filter="table1">
        </table>
    </div>
</div>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
{/block}
{block name="js"}
<script type="text/javascript">
    layui.use(['table'], function () {
        var $ = layui.$;
        var table = layui.table;
        var token = layui.sessionData('token').token;
        var inittable = table.render({
            elem: '#table1'
            , height: '800'
            , url: '{:url("tdmin/Article/getArticleList")}?token=' + token //数据接口
            , page: true //开启分页
            , limit: 20
            , size: 'lg'
            , height: '780'
            , cols: [[
                {field: 'id', title: 'ID', width: 70, fixed: 'left'}
                , {field: 'title', title: '标题'}
                , {field: 'sort', title: '排序'}
                , {field: 'create_time', title: '创建时间'}
                , {fixed: 'right', 'title': '操作', width: 220, align: 'center', templet: '#barDemo'}
            ]]
        });
        // 编辑的弹窗和删除
        table.on('tool(table1)', function (obj) {
            var data = obj.data; //获得当前行数据
            if (obj.event === 'edit') {
                window.location.href = "{:url('editArticle')}?id=" + data.id;
            } else if (obj.event === 'del') {
                layer.confirm('您确定要删除该文章吗？', function (index) {
                    var loading = layer.load(1, {shade: [0.1, '#fff']});
                    $.post("{:url('tdmin/Article/delArticle')}", {id: data.id, token: token}, function (res) {
                        layer.close(loading);
                        if (res.code === 0) {
                            layer.msg(res.msg, {time: 1000, icon: 1});
                            obj.del();
                        } else {
                            layer.msg('操作失败！', {time: 1000, icon: 2});
                        }
                    });
                    layer.close(index);
                });
            }
        });
    });
</script>
{/block}
